#include <stdio.h>
#include "somb.h"

void test_method_service_provider(uint32_t handle,uint32_t ServiceId,void*MthReqData,uint32_t MthReqDataLen)
{
    printf("test2 recv ServiceId=%08x handle=%08x datalen=%d\n",ServiceId,handle,MthReqDataLen);
}
int32_t test_event_on_listen_cb(uint32_t ServiceId,void**EvtData,uint32_t *EvtDataLen)
{
    /*if this function is called, that means some other consumer was listenning to BASICCOM_SID_EVENT_TEST*/
    printf("test2 recv Event ServiceId=0x%08x was being listened \n", ServiceId);
    
    /* prepare the memory space for the event data */
    *EvtData = SOMB_ALLOC(32);
    if(NULL == *EvtData)
    {
        printf("### malloc failed\n");
        return SOMB_FAIL;
    }
    memset(*EvtData, 0, 32);

    /* prepare the event data */
    *EvtDataLen = sprintf((char *)*EvtData, "%08x is ready!", ServiceId);

    /*don't free the memory allocated by EvtData if return SOMB_OK, it will auto freed by somb*/
    return SOMB_OK;
}

int32_t deploy_service(void)
{
    void**Instance = NULL;
    Instance = vSOMB_Service_Instance_New();
    if(NULL == Instance)
    {
        printf("new service instance failed");
        return -1;
    }


    int ret = 0;
    uint8_t service_functon_id = 0x01;
    uint8_t service_sub_functon_id = 0x01;
    uint32_t service_id = 0;

    for(int loop=0; loop < 512; loop++)
    {
        if(loop <= 63)
        {
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop;
        }
        else if(loop <= 127)
        {
            service_functon_id = 0x01;
            service_sub_functon_id = 0x02;
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop;
        }
        else if(loop <= 255)
        {
            service_functon_id = 0x02;
            service_sub_functon_id = 0x01; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 
        else if(loop <= 383)
        {
            service_functon_id = 0x02;
            service_sub_functon_id = 0x02; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 
        else 
        {
            service_functon_id = 0x03;
            service_sub_functon_id = 0x03; 
            service_id =  (service_functon_id << 24) | (service_sub_functon_id << 16) | loop; 
        } 


        if (loop%2 == 0)  /* 偶数 是 METHOD类型服务*/
        { 
            ret = i32SOMB_Service_Add(Instance, service_id, SOMB_SVC_TYPE_METHOD,test_method_service_provider); 
        } 
        else  /* 奇数 是 EVENT类型服务*/
        { 
            ret = i32SOMB_Service_Add(Instance, service_id, SOMB_SVC_TYPE_EVENT,test_event_on_listen_cb); 
        }

        if(SOMB_OK != ret)
        {
            printf("somb add service(%08x) failed\n", loop);
        }
    }

    ret = i32SOMB_Service_Instance_Deploy(Instance);
    if(SOMB_OK != ret)
    {
        printf("test2 server somb service deploy failed\n");
        return -1;
    }
    else 
    {
        printf("test2 server somb service deploy success\n");
    }


    ret = i32SOMB_Service_Instance_Destroy(Instance);
    if(SOMB_OK != ret)
    {
        printf("somb service destory failed\n");
        return -1;
    }

    return 0;
}

void trigger_event_service(void)
{
    uint32_t ServiceId ;
    char EventData[32] = {0};

    printf("please input an odd service id:\n");
    scanf("%08x", &ServiceId);
    if(ServiceId%2 == 0)
    {
        printf("please input odd service id\n");
        return;
    }

    sprintf(EventData, "0x%08x is ready!", ServiceId);
    
    int32_t ret = i32SOMB_Event_Trigger(ServiceId, EventData, strlen(EventData));
    if(SOMB_OK != ret)
    {
        printf("trigger event service id(%08x) failed\n", ServiceId);
    }
    else
    {
        printf("trigger event service id(%08x) success\n", ServiceId);
    }

}


void print_usage()
{
    printf("1:deploy service\n");
    printf("2:trigger event service\n");

}
int main(void)
{
    uint32_t SubNsId;
    int32_t ret = i32SOMB_SubNs_Init(&SubNsId);
    if(SOMB_OK != ret)
    {
        printf("%s somb sub ns init failed\n",__FUNCTION__ );
        return -1;
    }
    printf("SubNs=%d SOMB Init success\n",SubNsId);


    while(1)
    {
        print_usage();
        printf("chose your test index:\n");
        int cmd;
        scanf("%d",&cmd);
        printf("your chose is %d \n",cmd);
        switch(cmd)
        {
            case 1: /* deploy service */
                deploy_service();
                break;
            case 2: /* trigger event service */
                trigger_event_service();
                break;
        }
    }

    return 0;
}
